package com.zb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.Order;
import com.zb.mapper.OrderMapper;
import com.zb.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveOrders(List<Order> orderList) {
        if (orderList == null || orderList.isEmpty()) {
            return true;
        }
        
        // 分批次插入，避免SQL过长
        int batchSize = 1000;
        int total = orderList.size();
        int batches = (total + batchSize - 1) / batchSize;
        
        for (int i = 0; i < batches; i++) {
            int start = i * batchSize;
            int end = Math.min((i + 1) * batchSize, total);
            baseMapper.batchInsert(orderList.subList(start, end));
        }
        
        return true;
    }

    @Override
    public long countOrdersByMonthAndTenant(Long tenantId, String yearMonth) {
        // 实际应用中需要根据月份和租户ID查询订单数量
        // 这里仅作示例
        return 0;
    }
}
